package de.milo.game;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import de.milo.map.Map;
import de.milo.map.MapViewer;
import de.milo.map.tile.GroundTile;
import de.milo.test.TestMapGenerator;

public class MiloGame extends BasicGame
{
	private Map testMap;
	private MapViewer view;
	private Image floorImage;
	private Image wallImage;
	private Image emptyImage;
	private int viewX;
	private int viewY;
	private int viewWidth;
	private int viewHeight;
	private float viewSpeed;
	private float mapViewerX;
	private float mapViewerY;
	private int mapViewerMaxWidth;
	private int mapViewerMaxHeight;
	
	public MiloGame(String title)
	{
		super(title);
	}
	
	@Override
	public void init(GameContainer app) throws SlickException
	{
		app.setTargetFrameRate(60);
		app.setShowFPS(false);
		floorImage = new Image("res/bilder/floor.png");
		wallImage = new Image("res/bilder/wall.png");
		emptyImage = new Image("res/bilder/empty.png");
		GroundTile floor = new GroundTile(floorImage);
		GroundTile wall = new GroundTile(wallImage);
		GroundTile empty = new GroundTile(emptyImage);
		TestMapGenerator mapGenerator = new TestMapGenerator(floor, wall, empty);
		int f = TestMapGenerator.FLOOR_ID;
		int w = TestMapGenerator.WALL_ID;
		int e = TestMapGenerator.EMPTY_ID;
		testMap = mapGenerator.generate(new int[][]{ {w, w, w, w, w, w, w, e, e, e, e, e, e, e, e},
				                                     {w, f, f, f, f, f, w, e, e, e, e, e, e, e, e},
                                                     {w, f, f, f, f, f, w, e, e, e, e, w, w, w, w},
                                                     {w, f, f, f, f, f, w, e, e, e, e, w, f, f, w},
                                                     {w, w, w, f, w, w, w, e, e, e, e, w, f, f, w},
                                                     {e, e, w, f, w, e, e, e, e, e, e, w, f, f, w},
                                                     {e, e, w, f, w, e, w, w, w, w, w, w, f, f, w},
                                                     {e, e, w, f, w, w, w, f, f, f, f, f, f, f, w},
                                                     {e, e, w, f, f, f, f, f, f, f, w, w, w, w, w},
                                                     {e, e, w, f, w, w, w, f, f, f, w, e, e, e, e},
                                                     {e, e, w, f, w, e, w, f, f, f, w, e, e, e, e},
                                                     {w, w, w, f, w, e, w, w, w, f, w, w, w, w, e},
                                                     {w, f, f, f, w, e, e, e, w, f, f, f, f, w, e},
				                                     {w, f, f, w, w, e, e, e, w, f, f, f, f, w, e},
				                                     {w, w, w, w, e, e, e, e, w, w, w, w, w, w, e}});
		view = new MapViewer(testMap, 48, 48);
		viewX = -12;
		viewY = 0;
		viewWidth = 384;
		viewHeight = 384;
		viewSpeed = 0.015f;
		view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		
		mapViewerX = 10.f;
		mapViewerY = 10.f;
		mapViewerMaxWidth = app.getWidth() - 20;
		mapViewerMaxHeight = app.getHeight() - 20;
	}
	
	@Override
	public void update(GameContainer app, int delta) throws SlickException
	{
		Input input = app.getInput();
		if(input.isKeyPressed(Input.KEY_ESCAPE)) {
			app.exit();
		}
		
		int change = (int)(10 * delta * viewSpeed);
		if(input.isKeyDown(Input.KEY_A)) {
			viewWidth -= change;
			if(viewWidth < 1) {
				viewWidth = 1;
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
		if(input.isKeyDown(Input.KEY_D)) {
			viewWidth += change;
			if(viewWidth > mapViewerMaxWidth) {
				viewWidth = mapViewerMaxWidth;
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
		if(input.isKeyDown(Input.KEY_W)) {
			viewHeight -= change;
			if(viewHeight < 1) {
				viewHeight = 1;
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
		if(input.isKeyDown(Input.KEY_S)) {
			viewHeight += change;
			if(viewHeight > mapViewerMaxHeight) {
				viewHeight = mapViewerMaxHeight;
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
		
		if(input.isKeyDown(Input.KEY_LEFT)) {
			viewX -= change;
			if(viewX < -viewWidth) {
				viewX = -viewWidth;
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
		if(input.isKeyDown(Input.KEY_RIGHT)) {
			viewX += change;
			if(viewX > view.getMapPixelWidth()) {
				viewX = view.getMapPixelWidth();
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
		if(input.isKeyDown(Input.KEY_UP)) {
			viewY -= change;
			if(viewY < -viewHeight) {
				viewY = -viewHeight;
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
		if(input.isKeyDown(Input.KEY_DOWN)) {
			viewY += change;
			if(viewY > view.getMapPixelHeight()) {
				viewY = view.getMapPixelHeight();
			}
			view.setViewportInPixels(viewX, viewY, viewWidth, viewHeight);
		}
	}
	
	@Override
	public void render(GameContainer app, Graphics g) throws SlickException
	{
		view.draw(mapViewerX, mapViewerY);
		g.drawRect(mapViewerX, mapViewerY, view.getViewportWidth(), view.getViewportHeight());
	}
}
